package com.joysuch.wwyt.report.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.joysuch.wwyt.report.ReportAPIResultBean;

import com.joysuch.wwyt.report.api.service.RiskPointAPIService;
import com.joysuch.wwyt.report.bean.*;

import com.joysuch.wwyt.report.bean.dto.ReportDeviceRiskEvaluationRecordDto;
import com.joysuch.wwyt.report.bean.dto.ReportJobRiskEvaluationRecordDto;
import com.joysuch.wwyt.report.bean.dto.ReportRiskAnalyzeListDto;
import com.joysuch.wwyt.report.bean.dto.ReportRiskEvaIdentifyDto;
import com.joysuch.wwyt.report.service.ReportRiskPointService;
import com.joysuch.wwyt.report.service.RiskControlService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;


/**
 *  积木报表API 接口 ： 风险点清单
 */

@RestController
@RequestMapping("/report-api/risk-point")
@Api(tags = "报表-风险点清单")
public class RiskPointAPIController {

    @Autowired
    private RiskPointAPIService riskPointAPIService;
    @Autowired
    private RiskControlService riskControlService;
    @Autowired
    private ReportRiskPointService reportRiskPointService;

    @GetMapping("/getRiskControlList")
    @ApiOperation(value = "风险分级管控清单")
    public ReportAPIResultBean riskControl(@RequestParam(name = "pageNo",defaultValue = "1")Long pageNo,
                                           @RequestParam(name = "pageSize",defaultValue = "10000")Long pageSize,
                                           Long departId, Long riskLevel) {
        // 未选择部门
        if (departId == null || departId == 0) {
            return new ReportAPIResultBean(new ArrayList<ReportRiskPointBean>(), 0L, 0L);
        }
        Page<RiskControlListBean> riskControls = riskControlService.findByResDepartAndControlLevel(pageNo, pageSize, departId, riskLevel);
        // 总条数可整除页数直接相除返回，否则相除加一再返回
        long pageCount = riskControls.getTotal() % riskControls.getSize() == 0 ?
                riskControls.getTotal() / riskControls.getSize() : riskControls.getTotal() / riskControls.getSize() + 1;
        return new ReportAPIResultBean(riskControls.getRecords(), pageCount, riskControls.getTotal());
    }

    @GetMapping("/page")
    @ApiOperation(value = "风险点清单(总)")
    public ReportAPIResultBean list(@RequestParam(value = "departId") Long departId,
                                    @RequestParam(name = "pageNo",defaultValue = "1")Long pageNo,
                                    @RequestParam(name = "pageSize",defaultValue = "10000")Long pageSize) {
        // 未选择部门
        if (departId == null || departId == 0) {
            return new ReportAPIResultBean(new ArrayList<ReportRiskPointBean>(), 0L, 0L);
        }
        ReportRiskPointSearchBean searchBean = new ReportRiskPointSearchBean();
        searchBean.setDepartId(departId);
        searchBean.setPageNo(pageNo);
        searchBean.setPageSize(pageSize);
        Page<RiskPointListBean> pageData = riskPointAPIService.selectRiskPointPage(searchBean);
        long pageCount = pageData.getTotal() % pageData.getSize() == 0 ?
                pageData.getTotal() / pageData.getSize() : pageData.getTotal() / pageData.getSize() + 1;
        return new ReportAPIResultBean(pageData.getRecords(), pageCount, pageData.getTotal());
    }

    @GetMapping("/getRiskList")
    @ApiOperation(value = "风险点清单-风险点类型")
    public ReportAPIResultBean getRiskList(@RequestParam(name = "pageNo",defaultValue = "1")Long pageNo,
                                           @RequestParam(name = "pageSize",defaultValue = "10000")Long pageSize,
                                           String pointType, String subType, Long departId, Long jobId) {
        // 未选择部门
        if (departId == null || departId == 0) {
            return new ReportAPIResultBean(new ArrayList<ReportRiskPointBean>(), 0L, 0L);
        }
        Page<ReportRiskPointBean> points = reportRiskPointService.getPointByDepartAndJobPaging(pageNo, pageSize,pointType, subType, departId, jobId);
        // 总条数可整除页数直接相除返回，否则相除加一再返回
        long pageCount = points.getTotal() % points.getSize() == 0 ?
                points.getTotal() / points.getSize() : points.getTotal() / points.getSize() + 1;
        return new ReportAPIResultBean(points.getRecords(), pageCount, points.getTotal() );
    }

    @GetMapping("/findRiskPointControlByDepart")
    @ApiOperation(value = "作业活动分级管控")
    public ReportAPIResultBean findRiskPointControlByDepart(@RequestParam(value = "departId") Long departId,
                                                            @RequestParam(value = "pointType") String pointType,
                                                            @RequestParam(name = "pageNo",defaultValue = "1")Long pageNo,
                                                            @RequestParam(name = "pageSize",defaultValue = "10000")Long pageSize) {
        // 未选择部门
        if (departId == null || departId == 0) {
            return new ReportAPIResultBean(new ArrayList<ReportRiskPointBean>(), 0L, 0L);
        }
        ReportRiskPointControlSearchBean searchBean = new ReportRiskPointControlSearchBean();
        searchBean.setDepartId(departId);
        searchBean.setPointType(pointType);
        searchBean.setPageNo(pageNo);
        searchBean.setPageSize(pageSize);
        Page<ReportRiskPointControlListBean> pageData = riskControlService.findRiskPointControlByDepart(searchBean);
        long pageCount = pageData.getTotal() % pageData.getSize() == 0 ?
                pageData.getTotal() / pageData.getSize() : pageData.getTotal() / pageData.getSize() + 1;
        return new ReportAPIResultBean(pageData.getRecords(), pageCount, pageData.getTotal());
    }

    @GetMapping("/findReportJobRiskEvaluationRecord")
    @ApiOperation(value = "作业活动风险评价记录")
    public ReportAPIResultBean findReportJobRiskEvaluationRecord(@RequestParam(name = "pageNo",defaultValue = "1")Long pageNo,
                                                                 @RequestParam(name = "pageSize",defaultValue = "10000") Long pageSize,
                                                                 @RequestParam(name = "departId") Long departId,
                                                                 @RequestParam(name = "jobId", required = false) Long jobId) {
        // 未选择部门
        if (departId == null || departId == 0) {
            return new ReportAPIResultBean(new ArrayList<ReportJobRiskEvaluationRecordDto>(), 0L, 0L);
        }
        Page<ReportJobRiskEvaluationRecordDto> pageData = riskControlService.findReportJobRiskEvaluationRecord(pageNo, pageSize, departId, jobId);
        return new ReportAPIResultBean(pageData.getRecords(), calculatePageCount(pageData), pageData.getTotal());
    }

    @GetMapping("/findReportDeviceRiskEvaluationRecord")
    @ApiOperation(value = "设备设施风险评价记录")
    public ReportAPIResultBean findReportDeviceRiskEvaluationRecord(@RequestParam(name = "pageNo",defaultValue = "1")Long pageNo,
                                                                 @RequestParam(name = "pageSize",defaultValue = "10000") Long pageSize,
                                                                 @RequestParam(name = "departId") Long departId,
                                                                 @RequestParam(name = "jobId", required = false) Long jobId) {
        // 未选择部门
        if (departId == null || departId == 0) {
            return new ReportAPIResultBean(new ArrayList<ReportDeviceRiskEvaluationRecordDto>(), 0L, 0L);
        }
        Page<ReportDeviceRiskEvaluationRecordDto> pageData = riskControlService.findReportDeviceRiskEvaluationRecord(pageNo, pageSize, departId, jobId);
        return new ReportAPIResultBean(pageData.getRecords(), calculatePageCount(pageData), pageData.getTotal());
    }


    @GetMapping("/findReportRiskEvaluationIdentify")
    @ApiOperation(value = "设备设施-作业活动风险评估辨识")
    public ReportAPIResultBean findReportRiskEvaluationIdentify(@RequestParam(name = "pageNo",defaultValue = "1")Long pageNo,
                                                                @RequestParam(name = "pageSize",defaultValue = "10000") Long pageSize,
                                                                @RequestParam(name = "departId") Long departId,
                                                                @RequestParam(name = "jobId", required = false) Long jobId,
                                                                @RequestParam(name = "pointType", required = false) String pointType) {
        // 未选择部门
        if (departId == null || departId == 0) {
            return new ReportAPIResultBean(new ArrayList<ReportRiskEvaIdentifyDto>(), 0L, 0L);
        }
        Page<ReportRiskEvaIdentifyDto> pageData = riskControlService.findReportRiskEvaluationIdentify(pageNo, pageSize, departId, jobId, pointType);
        return new ReportAPIResultBean(pageData.getRecords(), calculatePageCount(pageData), pageData.getTotal());
    }

    @GetMapping("/findRiskAnalyzeList")
    @ApiOperation(value = "风险分析清单")
    public ReportAPIResultBean findRiskAnalyzeList(@RequestParam(name = "pageNo",defaultValue = "1")Long pageNo,
                                                   @RequestParam(name = "pageSize",defaultValue = "10000") Long pageSize,
                                                   String orgCode,
                                                   Long tenentId) {
        Page<ReportRiskAnalyzeListDto> pageData = riskControlService.findRiskAnalyzeList(pageNo, pageSize, orgCode, tenentId);
        return new ReportAPIResultBean(pageData.getRecords(), calculatePageCount(pageData),pageData.getTotal());
    }


    private Long calculatePageCount(Page<?> pageData) {
        return pageData.getTotal() % pageData.getSize() == 0 ?
                pageData.getTotal() / pageData.getSize() : pageData.getTotal() / pageData.getSize() + 1;
    }
}
